﻿using LyyCMS.Core.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace LyyCMS.Core
{
    public partial class LyyCMSEntities:DbContext
    {
        public LyyCMSEntities(DbContextOptions<LyyCMSEntities> dbContextOptions) : base(dbContextOptions)
        {

        }

        public virtual DbSet<Blog> Blogs { get; set; }

        public virtual DbSet<Comments> Comments { get; set; }

        public virtual DbSet<User> Users { get; set; }

        public virtual DbSet<Article> Articles { get; set; }

        public virtual DbSet<ArticleCategory> ArticleCategories { get; set; }

        public virtual DbSet<Site> Sites { get; set; }

        public virtual DbSet<Channel> Channels { get; set; }


        //种子数据
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

            #region
            modelBuilder.Entity<Channel>(entity =>
            {
                entity.HasOne(d => d.Site)
                .WithMany(p => p.Channels)
                .HasForeignKey("SiteId");
            });

            modelBuilder.Entity<Site>().HasData(
                new Site
                {
                    Id = 1,
                    SiteName = "默认站点",
                    SiteType = SiteType.PC,
                    Name = "默认站点",
                    CompanyAddress = "公司地址",
                    Email = "10000@qq.com",
                    Logo = "",
                    Phone = "",
                    Tex = "",
                    Record = "",
                    CopyRight = "@版权所有",
                    SortId = 1,
                    IsDefault = true,
                    IsDeleted = false,
                    IsEnabled = true,
                    CreationTime = DateTime.Now,
                    CreatorUserId = 1,
                    DeleterUserId = 0,
                    DeletionTime = DateTime.MinValue,
                });

            #endregion

            #region
            modelBuilder.Entity<ArticleCategory>(entity =>
            {
                entity.HasOne(d => d.Channel)
                .WithMany(p => p.ArticleCategorys)
                .HasForeignKey("ChannelId");
            });
            modelBuilder.Entity<Channel>().HasData(
                new Channel
                {
                    Id = 1,
                    Title = "新闻资讯",
                    SiteId=1,
                    UseName="",
                    SortId = 1,
                    IsAnnex = false,
                    IsComment = false,
                    IsPics = false,
                    IsDeleted = false,
                    IsEnabled = true,
                    CreationTime = DateTime.Now,
                    CreatorUserId = 1,
                    DeleterUserId = 0,
                    DeletionTime = DateTime.MinValue,
                }, new Channel
                {
                    Id = 2,
                    Title = "购物商城",
                    SiteId = 1,
                    UseName = "",
                    SortId = 2,
                    IsAnnex = false,
                    IsComment = false,
                    IsPics = false,
                    IsDeleted = false,
                    IsEnabled = true,
                    CreationTime = DateTime.Now,
                    CreatorUserId = 1,
                    DeleterUserId = 0,
                    DeletionTime = DateTime.MinValue,
                }, new Channel
                {
                    Id = 3,
                    Title = "视频专区",
                    SiteId = 1,
                    UseName = "",
                    SortId = 3,
                    IsAnnex = false,
                    IsComment = false,
                    IsPics = false,
                    IsDeleted = false,
                    IsEnabled = true,
                    CreationTime = DateTime.Now,
                    CreatorUserId = 1,
                    DeleterUserId = 0,
                    DeletionTime = DateTime.MinValue,
                }, new Channel
                {
                    Id = 4,
                    Title = "图片分享",
                    SiteId = 1,
                    UseName = "",
                    SortId = 4,
                    IsAnnex = false,
                    IsComment = false,
                    IsPics = false,
                    IsDeleted = false,
                    IsEnabled = true,
                    CreationTime = DateTime.Now,
                    CreatorUserId = 1,
                    DeleterUserId = 0,
                    DeletionTime = DateTime.MinValue,
                }, new Channel
                {
                    Id = 5,
                    Title = "资源下载",
                    SiteId = 1,
                    UseName = "",
                    SortId = 5,
                    IsAnnex = false,
                    IsComment = false,
                    IsPics = false,
                    IsDeleted = false,
                    IsEnabled = true,
                    CreationTime = DateTime.Now,
                    CreatorUserId = 1,
                    DeleterUserId = 0,
                    DeletionTime = DateTime.MinValue,
                }, new Channel
                {
                    Id = 6,
                    Title = "公司介绍",
                    SiteId = 1,
                    UseName = "",
                    SortId = 6,
                    IsAnnex = false,
                    IsComment = false,
                    IsPics = false,
                    IsDeleted = false,
                    IsEnabled = true,
                    CreationTime = DateTime.Now,
                    CreatorUserId = 1,
                    DeleterUserId = 0,
                    DeletionTime = DateTime.MinValue,
                }) ;


            #endregion


            modelBuilder.Entity<Blog>(entity =>
            {
                entity.Property(e => e.Title).IsRequired();
            });

            //设置一对多
            modelBuilder.Entity<Comments>(entity =>
            {
                entity.HasOne(d => d.Blog)
                .WithMany(p => p.Comments)
                .HasForeignKey("BlogId");
            });

            //User 默认数据
            modelBuilder.Entity<User>().HasData(
                new User { Id=1,UserName="Admin", UserPass="Admin", Crete_At=DateTime.Now, UserState=UserState.Active,UserType=UserType.Admin }
                );


            //Blog种子数据
            modelBuilder.Entity<Blog>().HasData(
                new Blog { Id = 1,UserId=1, Title = "net core", Content = "net core need", Category = BlogCategory.CSharp, Create_At = DateTime.Now },
                new Blog { Id = 2,UserId=1, Title = "ef core", Content = "ef core need", Category = BlogCategory.MSSQL, Create_At = DateTime.Now }

            );

            //设置一对多
            modelBuilder.Entity<Comments>(entity =>
            {
                entity.HasOne(d => d.Blog)
                .WithMany(p => p.Comments)
                .HasForeignKey("BlogId");
            });

            //为Comment 添加种子数据
            modelBuilder.Entity<Comments>().HasData(
                new Comments() { BlogId = 1, Id = 1, Title = "Net core is good", Comment = "我不会用", Create_At = DateTime.Now },
                new Comments() { BlogId = 1, Id = 2, Title = "Net core is not good", Comment = "我会用", Create_At = DateTime.Now },
                new Comments() { BlogId = 2, Id = 3, Title = "ef core is good", Comment = "我不会用11", Create_At = DateTime.Now },
                new Comments() { BlogId = 2, Id = 4, Title = "ef core is not good", Comment = "我会用22", Create_At = DateTime.Now }
                );



        }
    }
}
